package com.utils;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2019/9/1615:25
 */

public class NodeTest {

    public static void main(String[] args) {
        Node root = NodeTest.insert();
        NodeTest.last(root);
    }
    //初始化二叉树
    public static Node insert(){
        Node J = new Node(8, null, null);
        Node H = new Node(4, null, null);
        Node G = new Node(2, null, null);
        Node F = new Node(7, null, J);
        Node E = new Node(5, H, null);
        Node D = new Node(1, null, G);
        Node C = new Node(9, F, null);
        Node B = new Node(3, D, E);
        Node root = new Node(6, B, C);
        return root;
    }
    //前序遍历:跟-左-右
    public static void prev(Node root){
        //输出根节点
        System.out.println(root.getVal());
        //输出左节点
        Node left = root.getLeft();
        if(left != null){
            prev(left);//递归调用左节点
        }
        //输出右节点
        Node right = root.getRight();
        if(right !=null){
            prev(right);//递归调用右节点
        }
    }
    //中序遍历:左-跟-右
    public static void center(Node root){
        //输出左节点
        Node left = root.getLeft();
        if(left != null){
            center(left);//递归调用左节点
        }
        //输出根节点
        System.out.println(root.getVal());
        //输出右节点
        Node right = root.getRight();
        if(right !=null){
            center(right);//递归调用右节点
        }
    }
    //后序遍历:左-右-跟
    public static void last(Node root){
        //输出左节点
        Node left = root.getLeft();
        if(left != null){
            last(left);//递归调用左节点
        }
        //输出右节点
        Node right = root.getRight();
        if(right !=null){
            last(right);//递归调用右节点
        }
        //输出根节点
        System.out.println(root.getVal());
    }
}
